package cn.eonshine.cuba.lsms.modular.biz.service.impl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.eonshine.cuba.inf.common.util.DateUtils;
import cn.eonshine.cuba.inf.common.http.InfStatus;
import cn.eonshine.cuba.inf.modular.biz.entity.IndustryRealData;
import cn.eonshine.cuba.lsms.modular.CommonService;
import cn.eonshine.cuba.lsms.modular.basic.entity.BIndustry;
import cn.eonshine.cuba.lsms.modular.basic.service.IBIndustryService;
import cn.eonshine.cuba.lsms.modular.biz.entity.LsmsIndustryRealData;
import cn.eonshine.cuba.lsms.modular.biz.service.IIndustryRealDataService;
import cn.eonshine.cuba.lsms.modular.biz.service.IInfRealTimeDataOffService;
import cn.eonshine.cuba.lsms.modular.biz.service.ILsmsIndustryRealDataService;

@Service
public class IndustryRealDataService extends CommonService<IndustryRealData> implements IIndustryRealDataService {
	
	@Autowired
	private ILsmsIndustryRealDataService lsmsIndustryRealDataService;
	@Autowired
	private IBIndustryService industryService;
	
	private final String listUrl = "/industryRealData/list";
	private final String updateUrl = "/industryRealData/update";
	
	@Autowired
	private IInfRealTimeDataOffService infRealTimeDataOffService;
	
	@Override
	public void requestRealData() {
		requestData(listUrl, updateUrl, InfStatus.INF_LOG_TYPE.INDUSTRY_REAL_NUM);
	}

	@Override
	protected Object dataHandle(IndustryRealData obj) throws Exception {
		
		boolean isClosed=false;
		if(obj.getRptDate() != null) {
			isClosed = infRealTimeDataOffService.isClosed(obj.getRptDate());
		}else {
			// 没有业务日期的数据，不接收此数据。
//			throw new Exception("");
			return null;
		}
		// 数据关闭，不再接收此数据。
		if(isClosed) {
			return null;
		}
		
		if(StringUtils.isBlank(obj.getIndustryId())) {
			throw new Exception("Miss industry code: "+obj.toString());
		}
		BIndustry industry = industryService.getOne(new QueryWrapper<BIndustry>().lambda().eq(BIndustry::getCode, obj.getIndustryId()));
		if(industry == null) {
			throw new Exception("There is no industry in sistlog, do not match industry code: "+obj.getIndustryId());
		}
		LsmsIndustryRealData info = null;
		try {
			// depot product industry report-date unique data
			QueryWrapper<LsmsIndustryRealData> query = new QueryWrapper<LsmsIndustryRealData>();
			query.lambda().eq(LsmsIndustryRealData::getDepotId, obj.getDepotKey())
					.eq(LsmsIndustryRealData::getOilId, obj.getOilKey())
					.eq(LsmsIndustryRealData::getIndustryId, industry.getId())
					.eq(LsmsIndustryRealData::getRptDate, obj.getRptDate());
			
			info = lsmsIndustryRealDataService.getOne(query);
			boolean isInsert = false;
			if(info ==null) {
				// Add Data
				info = new LsmsIndustryRealData();
				isInsert = true;
			}
			
			info.setDepotId(obj.getDepotKey());
			info.setOilId(obj.getOilKey());
			info.setRptDate(obj.getRptDate());
			info.setQuantity(BigDecimal.valueOf(obj.getQuantity()));
			info.setIndustryId(obj.getIndustryId());
			
			Calendar cal = Calendar.getInstance();
			cal.setTime(obj.getRptDate());
			cal.set(Calendar.DAY_OF_MONTH, 1);
			info.setReportMonth(cal.getTime());
			
			info.setCreateBy(InfStatus.CREATE_BY);
			info.setUpdateBy(InfStatus.UPDATE_BY);
			if(isInsert) {
				info.setCreateDate(DateUtils.getCubaDate(Calendar.getInstance().getTime()));
			}
			info.setUpdateDate(DateUtils.getCubaDate(Calendar.getInstance().getTime()));
			
			if (isInsert) {
				lsmsIndustryRealDataService.save(info);
			}else {
				lsmsIndustryRealDataService.updateById(info);
			}
		} catch(Exception e) {
			throw new Exception("There is mulity result in LSMS : "+obj.toString());
		}
		
		return info;
	}

	@Override
	protected List<IndustryRealData> feadBackDataHandle(Object obj, IndustryRealData t,
			List<IndustryRealData> feadBackDataLists) throws Exception {
		LsmsIndustryRealData info  = (LsmsIndustryRealData) obj;
		t.setId(info.getId());
		t.setLsmsUpDate(InfStatus.LSMS_UPDATE_EXTRACT_YES);
		feadBackDataLists.add(t);
		return feadBackDataLists;
	}

}
